package com.bidepan.spring6.tx.dao;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookDaoImpl implements BookDao{
//    注入jdbcTemplate来操作数据库
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //根据图书id查询图书价格
    @Override
    public Integer getPriceByBookId(Integer bookId) {
        String sql = "Select price from t_book where id = ?";
        Integer price = jdbcTemplate.queryForObject(sql,Integer.class,bookId);
        return price;
    }

    //更新图书库存
    @Override
    public void updateStock(Integer bookId) {
        String sql = "UPDATE t_book SET stock = stock - 1 WHERE id =?";
        jdbcTemplate.update(sql, bookId);
    }

//    更新用户余额
    @Override
    public void updateUserBalance(Integer userId, Integer price) {
        String sql = "UPDATE t_user SET balance = balance -? WHERE id =?";
        jdbcTemplate.update(sql, price, userId);
    }
}
